package com.kingmed.kmss.framework.stream.mq.producer;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import com.kingmed.kmss.common.utils.DateTimeUtil;
import com.kingmed.kmss.framework.stream.mq.MQMessage;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MQProducer {

    @Autowired
    private StreamBridge streamBridge;

    public <T> void sendMsg(String bindingName, String message, T bizObj, String sender) {
        // 构建消息对象
        MQMessage<T> msg = new MQMessage<T>()
            .setMsgId(UUID.randomUUID().toString())
            .setSendTime(DateTimeUtil.getCurrentDateTimeStr())
            .setSender(sender)
            .setMsgText(message)
            .setBizObject(bizObj);
        
        log.info("准备发送消息: bingding: {}" + msg, bindingName);
        Boolean succeed = streamBridge.send(bindingName, MessageBuilder.withPayload(msg).build());
        log.info("发送消息结果: " + (succeed?"成功":"失败"));
    }

}
